package com.ffcs.web.controllers;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ffcs.web.model.dto.BaseResponse;
import com.ffcs.web.model.entity.WechatCanyueyue;
import com.ffcs.web.services.WechatCanyueyueService;
import com.ffcs.web.utils.XingqiUtil;

@Controller
public class WorkTimeController {

	/**
	 * 前往预约配置页面
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private Logger logger = Logger.getLogger(WorkTimeController.class);
	
	@Autowired
	private WechatCanyueyueService wechatCanyueyueService;
	
	/**
	 * 前往预约界面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("gotoyuyuejsp")
	public void gotoyuyuejsp(HttpServletRequest request,HttpServletResponse response,String tel,String errorMsg) throws ServletException, IOException{
		String xingqi="";
		boolean b=false;
		boolean b2=false;
		if(tel.length()==11){
			xingqi=XingqiUtil.getWeekOfDate(new Date());
			b=true;
		}else{
			xingqi=tel.substring(tel.length()-1,tel.length());
			if(xingqi.equals(XingqiUtil.getWeekOfDate(new Date()))){
				b2=true;
			}
		}
//		Date date0=new Date();
		
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(new Date());
//		calendar.add(Calendar.DATE, +6);
//		Date date=calendar.getTime();
//		bookTimes=sdf.format(date);
		List<String> listStr=XingqiUtil.getDayList(Integer.parseInt(xingqi));
		for (int i=1;i<=listStr.size();i++) {
			request.setAttribute("d"+i,listStr.get(i-1));
		}
//		XingqiUtil.getDay();
		
		request.setAttribute("xingqi",xingqi);
		tel=tel.substring(0,11);
		request.setAttribute("tel",tel);
		request.setAttribute("errorMsg",errorMsg);
		String counseTel=tel;
		List<WechatCanyueyue> canYuyueList=null;
		if(b){
			canYuyueList=wechatCanyueyueService.getToday(counseTel,"100");
		}else{
			canYuyueList=wechatCanyueyueService.getToday(counseTel,xingqi);
		}
		Date d = new Date();
		int hours = d.getHours();
		for (WechatCanyueyue wechatCanyueyue : canYuyueList) {
			if(b2){
				if(Integer.parseInt(wechatCanyueyue.getCanTime())>hours){
					logger.info("counseTel="+counseTel+"已经设置了"+wechatCanyueyue.getCanDate()+"|"+wechatCanyueyue.getCanTime()+":00");
					request.setAttribute("c"+wechatCanyueyue.getCanTime(),wechatCanyueyue.getCanTime());
				}
			}else{
				if(b){
					if(Integer.parseInt(wechatCanyueyue.getCanTime())>hours){
						logger.info("counseTel="+counseTel+"已经设置了"+wechatCanyueyue.getCanDate()+"|"+wechatCanyueyue.getCanTime()+":00");
						request.setAttribute("c"+wechatCanyueyue.getCanTime(),wechatCanyueyue.getCanTime());
					}
				}else{
					logger.info("counseTel="+counseTel+"已经设置了"+wechatCanyueyue.getCanDate()+"|"+wechatCanyueyue.getCanTime()+":00");
					request.setAttribute("c"+wechatCanyueyue.getCanTime(),wechatCanyueyue.getCanTime());
				}
			}
		}
		request.getRequestDispatcher("yuyue.jsp").forward(request, response);
	}
	
	/**
	 * 获取今天设置预约时间的画面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("gotoCanYuyue")
	public void gotoCanYuyue(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String xingqi=request.getParameter("xingqi");
		boolean b=false;
		if(null==xingqi || "".equals(xingqi)){
			xingqi=XingqiUtil.getWeekOfDate(new Date());
			b=true;
		}
		request.setAttribute("xingqi",xingqi);
		
		List<String> listStr=XingqiUtil.getDayList(Integer.parseInt(xingqi));
		for (int i=1;i<=listStr.size();i++) {
			request.setAttribute("d"+i,listStr.get(i-1));
		}
		
		String counseTel=(String) request.getSession().getAttribute("counseTel");
		List<WechatCanyueyue> canYuyueList=null;
		if(b){
			canYuyueList=wechatCanyueyueService.getToday(counseTel,"100");
		}else{
			canYuyueList=wechatCanyueyueService.getToday(counseTel,xingqi);
		}
		for (WechatCanyueyue wechatCanyueyue : canYuyueList) {
			logger.info("counseTel="+counseTel+"已经设置了"+wechatCanyueyue.getCanDate()+"|"+wechatCanyueyue.getCanTime()+":00");
			request.setAttribute("c"+wechatCanyueyue.getCanTime(),wechatCanyueyue.getCanTime());
		}
		request.getRequestDispatcher("business/canYuyue.jsp").forward(request, response);
	}
	
	/**
	 * 设置可预约时间
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("gotoSetCanYuyue")
	@ResponseBody
	public BaseResponse gotoSetCanYuyue(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		BaseResponse baseResponse=new BaseResponse();
		
		String totalTime=request.getParameter("totalTime");
		String week=request.getParameter("week");
		String counseTel=(String) request.getSession().getAttribute("counseTel");
		try {
			wechatCanyueyueService.setCanYuyue(counseTel,totalTime,week);
		} catch (Exception e) {
			logger.info("counseTel="+counseTel+"设置预约时间"+totalTime+"失败");
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("设置预约时间失败");
			return baseResponse;
		}
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
}
